package com.zhang.controller;

import com.alibaba.fastjson.JSON;
import com.zhang.entity.co.MachineResult;
import com.zhang.entity.response.ResponseResult;
import com.zhang.entity.vo.*;
import com.zhang.service.MachineService;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zhang.common.Constant.SUCCESS;


/**
 * User: 张
 * Date: 2024-11-14
 * Time: 17:03
 * Description:
 */
@RestController
public class MachineController {
    @Autowired
    private MachineService deviceOperate;

    @RequestMapping("/hello")
    public ResponseResult<String> Hello(@Param("hello")Integer hello) throws Exception {
        if (hello == 1)
            throw new Exception("测试");
        return ResponseResult.success("Hello World");
    }
    @RequestMapping("/init")
    public ResponseResult<Object> Init() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.Init(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/check_material")
    public ResponseResult<Object> CheckMaterial() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.CheckMaterial(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }

    @PostMapping("/control_conveyor_belt")
    public ResponseResult<Object> ControlConveyorBelt(@RequestBody ControlConveyorBeltBody controlConveyorBeltBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ControlConveyorBelt(controlConveyorBeltBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }

    @PostMapping("/control_light")
    public ResponseResult<String> ControlLight(@RequestBody ControlLightBody controlLightBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ControlLight(controlLightBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @PostMapping("/control_buzzer")
    public ResponseResult<String> ControlBuzzer(@RequestBody ControlBuzzerBody controlBuzzerBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ControlBuzzer(controlBuzzerBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/push_material")
    public ResponseResult<Object> PushMaterial() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.PushMaterial(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/run_conveyor_belt")
    public ResponseResult<Object> RunConveyorBelt() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.RunConveyorBelt(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @PostMapping("/set_axis_speed")
    public ResponseResult<Object> SetAxisSpeed(@RequestBody SetAxisSpeedBody setAxisSpeedBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.SetAxisSpeed(setAxisSpeedBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/move_axis_to_origin")
    public ResponseResult<Object> MoveAxisToOrigin(@RequestBody MoveAxisToOriginBody moveAxisToOriginBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.MoveAxisToOrigin(moveAxisToOriginBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/move_axis")
    public ResponseResult<Object> MoveAxis(@RequestBody MoveAxisBody moveAxisBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.MoveAxis(moveAxisBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/stop_axis")
    public ResponseResult<Object> StopAxis(@RequestBody StopAxisBody stopAxisBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.StopAxis(stopAxisBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/toggle_switches")
    public ResponseResult<Object> ToggleSwitches() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ToggleSwitches(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/control_axis_position")
    public ResponseResult<Object> ControlAxisPosition(@RequestBody ControlAxisPositionBody controlAxisPositionBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ControlAxisPosition(controlAxisPositionBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/read_axis_position")
    public ResponseResult<Object> ReadAxisPosition(@RequestBody ReadAxisPositionBody readAxisPositionBody) throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.ReadAxisPosition(readAxisPositionBody), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }
    @RequestMapping("/get_conveyor_status")
    public ResponseResult<Object> GetConveyorStatus() throws Exception {
        MachineResult machineResult = JSON.parseObject(deviceOperate.GetConveyorStatus(), MachineResult.class);
        return machineResult.getState().equals(SUCCESS) ? ResponseResult.success(machineResult.getData()) : ResponseResult.error(machineResult.getData());
    }














}
